﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Components;
using Model;
using MsDAL;
using System.ComponentModel;
using ServiceHelper.Doc;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Sto8a00Manager : Sto8a00Base
    {
        private static Sto8a00Manager _entity;

        public static Sto8a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Sto8a00Manager();
                return _entity;
            }
        }

        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            if ("A" == action)
            {
                //表头
                var helper = new PurchaseReturnManager();

                var sto8a00 = helper.CreateDefaultModel();
                CommonUtil.GetInstance.CopyProperties(tb, sto8a00);

                helper.Parse(sto8a00);

                foreach (DataRow row in dtl.Rows)
                {
                    var sto801 = new Sto8a01();
                    CommonUtil.GetInstance.CopyProperties(sto801, row);

                    helper.ParseRow(sto801);
                }

                return helper.Save();
            }
            else if ("M" == action)
            {
                var helper = new PurchaseReturnManager();

                var listid = Validation.ToString(tb["listid"]);
                var sto8a00 = GetModelById(listid);

                CommonUtil.GetInstance.CopyProperties(tb, sto8a00);

                helper.Parse(sto8a00);

                foreach (DataRow row in dtl.Rows)
                {
                    var dtlid = Validation.ToString(row["dtlid"]);
                    Sto8a01 sto801 = null;

                    if (string.IsNullOrEmpty(dtlid))
                        sto801 = new Sto8a01();
                    else
                        sto801 = Sto8a01Manager.Entity.GetModelById(dtlid);

                    CommonUtil.GetInstance.CopyProperties(sto801, row);

                    helper.ParseRow(sto801);
                }

                if (null != deldata && deldata.Rows.Count > 0)
                {
                    foreach (DataRow row in deldata.Rows)
                    {
                        var dtlid = Validation.ToString(row["dtlid"]);
                        if (string.IsNullOrEmpty(dtlid))
                            continue;

                        var sto801 = Sto8a01Manager.Entity.GetModelById(dtlid);

                        sto801.Status = Components.Enum.ModelStatus.Delete;
                        helper.ParseRow(sto801);
                    }
                }

                return helper.Save();
            }

            return null;
        }

        public string MeditSaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            if ("A" == action) // 发货单调整
            {
                //表头
                var helper = new SendAdjustManager();

                var sto8a00 = helper.CreateDefaultModel();
                CommonUtil.GetInstance.CopyProperties(tb, sto8a00);
                sto8a00.Warehouseid = Validation.ToString(tb["storageid"]);

                if (string.IsNullOrEmpty(sto8a00.Warehouseid))
                    return "未设置发货仓库，不能做发货调整";

                helper.Parse(sto8a00);

                foreach (DataRow row in dtl.Rows)
                {
                    var sto801 = new Sto8a01();
                    CommonUtil.GetInstance.CopyProperties(sto801, row);

                    sto801.Preqty1 = Validation.ToDouble(row["dtlqty"]);
                    sto801.Preqty2 = Validation.ToDouble(row["ppreqty"]);
                    sto801.Preprice1 = Validation.ToDouble(row["dtlprice"]);
                    sto801.Preamt1 = Validation.ToDouble(row["dtltotalamt"]);

                    sto801.Dtlqty = sto801.Actqty1 + sto801.Actqty2 - sto801.Preqty1 - sto801.Preqty2;
                    sto801.Dtltotalamt = sto801.Actamt1 - sto801.Preamt1;
                    if (!sto801.Dtlqty.Equals(0))
                    {
                        //计算调整的平均单价
                        sto801.Dtlprice = Math.Round(sto801.Dtltotalamt/sto801.Dtlqty, 3);
                    }

                    sto801.Dtlstockqty = sto801.Dtlqty;

                    helper.ParseRow(sto801);
                }

                return helper.Save();
            }

            return null;
        }

        public string MeditBackSaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            if ("A" == action) // 退货单调整
            {
                //表头
                var helper = new BackAdjustManager();

                var sto8a00 = helper.CreateDefaultModel();
                CommonUtil.GetInstance.CopyProperties(tb, sto8a00);
                sto8a00.Warehouseid = Validation.ToString(tb["storageid"]);

                if (string.IsNullOrEmpty(sto8a00.Warehouseid))
                    return "未设置退货仓库，不能做退货调整";

                helper.Parse(sto8a00);

                foreach (DataRow row in dtl.Rows)
                {
                    var sto801 = new Sto8a01();
                    CommonUtil.GetInstance.CopyProperties(sto801, row);

                    sto801.Preqty1 = Validation.ToDouble(row["dtlqty"]);
                    sto801.Preprice1 = Validation.ToDouble(row["dtlprice"]);
                    sto801.Preamt1 = Validation.ToDouble(row["dtltotalamt"]);

                    sto801.Dtlqty = sto801.Actqty1 - sto801.Preqty1;
                    sto801.Dtltotalamt = sto801.Actamt1 - sto801.Preamt1;

                    if (!sto801.Dtlqty.Equals(0))
                    {
                        //计算调整的平均单价
                        sto801.Dtlprice = Math.Round(sto801.Dtltotalamt/sto801.Dtlqty, 3);
                    }

                    sto801.Dtlstockqty = sto801.Dtlqty;

                    helper.ParseRow(sto801);
                }

                return helper.Save();
            }

            return null;
        }

        public string MeditAcceptSaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            if ("A" == action) // 退货单调整
            {
                //表头
                var helper = new AcceptAdjustManager();

                var sto8a00 = helper.CreateDefaultModel();
                CommonUtil.GetInstance.CopyProperties(tb, sto8a00);

                if (string.IsNullOrEmpty(sto8a00.Warehouseid))
                    return "未设置退货仓库，不能做退货调整";

                helper.Parse(sto8a00);

                foreach (DataRow row in dtl.Rows)
                {
                    var sto801 = new Sto8a01();
                    CommonUtil.GetInstance.CopyProperties(sto801, row);

                    sto801.Preqty1 = Validation.ToDouble(row["accqty"]);
                    sto801.Preqty2 = Validation.ToDouble(row["accpreqty"]);
                    sto801.Preprice1 = Validation.ToDouble(row["price"]);
                    sto801.Preamt1 = Validation.ToDouble(row["accamt"]);

                    sto801.Dtlqty = (sto801.Actqty1 + sto801.Actqty2) - (sto801.Preqty1 + sto801.Preqty2);
                    sto801.Dtltotalamt = Math.Round(sto801.Actamt1 - sto801.Preamt1, 3);

                    if (!sto801.Dtlqty.Equals(0))
                    {
                        //计算调整的平均单价
                        sto801.Dtlprice = Math.Round((sto801.Dtltotalamt) / sto801.Dtlqty, 3);
                    }

                    sto801.Dtlstockqty = sto801.Dtlqty;

                    helper.ParseRow(sto801);
                }

                return helper.Save();
            }

            return null;
        }
    }
}



